package com.wanjia.estate.service.httpservice;

import cn.hutool.json.JSONUtil;
import com.wanjia.estate.pojo.print.CommonResponse;
import com.wanjia.estate.pojo.print.DoorStatusResponse;
import com.wanjia.estate.pojo.print.PaperDrive;
import com.wanjia.estate.pojo.print.Stamp;
import com.wanjia.estate.properties.HttpRequestProperties;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class NewPrintHttpService extends AbstractHttpService {

    @Setter(onMethod_ = @Autowired)
    private HttpRequestProperties properties;

    /**
     * 盖章
     *
     * @param request 请求参数
     * @return 响应
     */
    public Stamp.Response newStamp(Stamp.Request request) {
        String url = properties.getNewStampUrl();
        log.debug("request url: {}", url);
        log.debug("request body: {}", JSONUtil.toJsonStr(request));

        RequestEntity<Stamp.Request> requestEntity = RequestEntity.post(url)
                .contentType(MediaType.APPLICATION_JSON)
                .body(request);
        ResponseEntity<String> entity = template.exchange(requestEntity, String.class);
        Stamp.Response response = parseObjResponseEntity(entity, Stamp.Response.class);
        log.debug("response body: {}", JSONUtil.toJsonStr(response));

        return response;
    }

    /**
     * 开启传动机构
     * @return
     */
    public CommonResponse newStartPrinterPaperDrive() {
        String url = properties.getNewStartPrinterPaperDriveUrl();
        log.debug("request url: {}", url);
        RequestEntity<Void> requestEntity = RequestEntity.post(url).build();
        ResponseEntity<String> entity = template.exchange(requestEntity, String.class);
        CommonResponse response = parseObjResponseEntity(entity, CommonResponse.class);
        log.debug("response body: {}", JSONUtil.toJsonStr(response));
        return response;
    }

    /**
     * 关闭传动机构
     */
    public CommonResponse newStopPrinterPaperDrive() {
        String url = properties.getNewStopPrinterPaperDriveUrl();
        log.debug("request url: {}", url);

        RequestEntity<Void> requestEntity = RequestEntity.delete(url).build();
        ResponseEntity<String> entity = template.exchange(requestEntity, String.class);
        CommonResponse response = parseObjResponseEntity(entity, CommonResponse.class);
        log.debug("response body: {}", JSONUtil.toJsonStr(response));

        return response;

    }

    /**
     * 打开出纸门
     * @return
     */
    public CommonResponse newOpenPaperDoor() {
        String url = properties.getNewOpenPaperDoorUrl();
        log.debug("request url: {}", url);

        RequestEntity<Void> requestEntity = RequestEntity.post(url).build();
        ResponseEntity<String> entity = template.exchange(requestEntity, String.class);
        CommonResponse response = parseObjResponseEntity(entity, CommonResponse.class);
        log.debug("response body: {}", JSONUtil.toJsonStr(response));

        return response;
    }

    public CommonResponse newRepeatOpenPaperDoor() {
        return this.newOpenPaperDoor();
    }

    /**
     *
     * 查询出纸门状态
     * @return
     */
    public DoorStatusResponse newStatusPaperDoor() {
        String url = properties.getNewStatusPaperDoorUrl();
        log.debug("request url: {}", url);

        RequestEntity<Void> requestEntity = RequestEntity.get(url).build();
        ResponseEntity<String> entity = template.exchange(requestEntity, String.class);
        DoorStatusResponse response = parseObjResponseEntity2(entity, DoorStatusResponse.class);
        log.debug("response body: {}", JSONUtil.toJsonStr(response));

        return response;
    }


    public void PaperStop() {
        for (int i = 0; i < 3; i++) {
            newStopPrinterPaperDrive();
        }
    }

    public void PaperDrive() {
        for (int i = 0; i < 3; i++) {
            newStartPrinterPaperDrive();
        }
    }



}
